//
//  PTStuLSPartJobDetailViewController.m
//  PlanTech
//  生活服务-兼职详情页
//  Created by ruifeng song on 2018/7/12.
//  Copyright © 2018年  androidlongs. All rights reserved.
//

#import "PTStuLSPartJobDetailViewController.h"
#import "PTStuLSPartJobDetailContentCell.h"
#import "PTStuLSMissionDetailInfoCell.h"
#import "PTStuLSReleaseDetailGradeCell.h"
#import "PTCoreManager+LifeService.h"
#import "PTCoreObject.h"
#import "PTCoreObject+LifeService.h"

@interface PTStuLSPartJobDetailViewController ()<UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate>

@property(nonatomic,strong)UITableView *partJobDetailTableView;
@property(nonatomic,strong)UIButton * bottomButton;
@property(nonatomic,assign)NSInteger status;

@property(nonatomic,strong)LifeServiceMissionDetailModel *missionDetailModel;

@end

@implementation PTStuLSPartJobDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"兼职详情";
    
    self.view.backgroundColor = BaseViewBackgroundColor;
    
    [self createView];
    
    [self requestData];
}

-(void)requestData
{
    WeakSelf;
    [[PTCoreManager shareInstance]performGetMyMissionDetailWithUUID:self.missionUUID Completion:^(id  _Nullable retObject, NSError * _Nullable retError) {
       
        if (retObject) {
            
            if ([retObject isKindOfClass:[PTMJsonResult class]]) {
                
            }
            else
            {
                 weakSelf.missionDetailModel = retObject;
                
                self.status = [weakSelf.missionDetailModel.status integerValue];
                //四种状态值
                if (self.status == PTLifeServiceMission_NormalStatus) {
                    
                    //待完成，可点击
                    self.bottomButton.backgroundColor = PTColor_349ef7;
                    [self.bottomButton setTitle:@"完成兼职" forState:UIControlStateNormal];
                    self.bottomButton.tag = PTLifeServiceMission_NormalStatus;
                }
                else if (self.status == PTLifeServiceMission_CommentStatus)
                {
                    //已评价-可删除
                    self.bottomButton.backgroundColor = PTColor_ff3a31;
                    [self.bottomButton setTitle:@"删除" forState:UIControlStateNormal];
                    self.bottomButton.tag = PTLifeServiceMission_FinishStatus;
                }
                else if (self.status == PTLifeServiceMission_WaitSolveStatus)
                {
                    //待处理，不可点击
                    self.bottomButton.backgroundColor = PTColor_d0d0d0;
                    [self.bottomButton setTitle:@"正在处理中" forState:UIControlStateNormal];
                    self.bottomButton.enabled = NO;
                }
                else if (self.status == PTLifeServiceMission_FinishStatus)
                {
                    //已完成，待评价
                    self.bottomButton.backgroundColor = PTColor_d0d0d0;
                    [self.bottomButton setTitle:@"等待对方评价" forState:UIControlStateNormal];
                    self.bottomButton.enabled = NO;
                    
                }
                
                [weakSelf.partJobDetailTableView reloadData];
            }
        }
       else
       {
           
       }
        
    }];
}

-(UITableView *)partJobDetailTableView
{
    if (_partJobDetailTableView == nil) {
        
        _partJobDetailTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight -TOP_HEIGHT - BOTTOM_HEIGHT - 70) style:UITableViewStylePlain];
        _partJobDetailTableView.delegate = self;
        _partJobDetailTableView.dataSource = self;
        _partJobDetailTableView.backgroundColor = [UIColor clearColor];
        _partJobDetailTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _partJobDetailTableView;
}

-(void)createView
{
    _bottomButton =[UIButton buttonWithType:UIButtonTypeCustom];
    [_bottomButton setFrame:CGRectMake(15, ScreenHeight - 40 - BOTTOM_HEIGHT - 30 - TOP_HEIGHT, ScreenWidth - 30,40)];
    _bottomButton.titleLabel.font = PTFONT_18;
    [_bottomButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _bottomButton.layer.cornerRadius = 20;
    _bottomButton.layer.masksToBounds = YES;
    [_bottomButton addTarget:self action:@selector(onClickBottomButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_bottomButton];
    
    [self.view addSubview:self.partJobDetailTableView];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.status == PTLifeServiceMission_NormalStatus || self.status == PTLifeServiceMission_FinishStatus || self.status == PTLifeServiceMission_WaitSolveStatus) {
        return 2;
    }

    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section ==0) {
        
        static NSString *ContentCellIdentifier = @"ContentCell";
        PTStuLSPartJobDetailContentCell * contentCell = [tableView dequeueReusableCellWithIdentifier:ContentCellIdentifier];
        if (contentCell == nil) {
            
            contentCell = [[PTStuLSPartJobDetailContentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ContentCellIdentifier];
            contentCell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        [contentCell updatePartJobDetailContentCellWith:self.missionDetailModel];
        return contentCell;
    }
    else if (indexPath.section ==1)
    {
        
        static NSString *JobInfoCellIdentifier = @"JobInfoCell";
        PTStuLSMissionDetailInfoCell * jobInfoCell = [tableView dequeueReusableCellWithIdentifier:JobInfoCellIdentifier];
        if (jobInfoCell == nil) {
            
            jobInfoCell = [[PTStuLSMissionDetailInfoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:JobInfoCellIdentifier];
            jobInfoCell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        [jobInfoCell updateMissionDetailInfoCellWith:self.missionDetailModel];
        return jobInfoCell;
    }
    else if (indexPath.section ==2)
    {
        static NSString *GradeCellIdentifier = @"GradeCell";
        PTStuLSReleaseDetailGradeCell * gradeCell = [tableView dequeueReusableCellWithIdentifier:GradeCellIdentifier];
        if (gradeCell == nil) {
            
            gradeCell = [[PTStuLSReleaseDetailGradeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:GradeCellIdentifier];
        }
        [gradeCell updateServiceGradeCell:self.missionDetailModel.rating];
        return gradeCell;
    }
    else
    {
        return [[UITableViewCell alloc]init];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
        {

            if (self.missionDetailModel==nil) {
                return (15 + 15 + 33 + 75 + 10 + 70);
            }
            else
            {
                NSString *remarkStr = self.missionDetailModel.remark;
                CGFloat remarkHeight = [remarkStr heightForFont:PTFONT_12 width:(ScreenWidth - 40 - 24 - 40)];
                if (remarkHeight == 0) {
                    
                    remarkHeight = 15;
                }
                
                NSString * addressStr  = self.missionDetailModel.employerAddress;
                CGFloat addressHeight = [addressStr heightForFont:PTFONT_12 width:ScreenWidth - 40 - 24 - 62];
                
               return (remarkHeight + addressHeight + 33 + 75 + 10 + 70);
            }
            
            break;
        }
        case 1:
            return 109;
            break;
        case 2:
            return 80;
            break;
    }
    return 0;
}

-(void)onClickBottomButton:(UIButton *)sender
{
    if (sender.tag == PTLifeServiceMission_FinishStatus) {
        
        //删除
        [self deleteMissonJob];
    }
    else if (sender.tag == PTLifeServiceMission_NormalStatus)
    {
        //完成
        [self finishMissionJob];
    }
}


//删除订单
-(void)deleteMissonJob
{
    UIAlertView * alertView  =[[UIAlertView alloc]initWithTitle:nil message:@"确定删除此订单？\n（删除后不可恢复）" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alertView.tag =22;
    [alertView show];
}

//完成兼职
-(void)finishMissionJob
{
    UIAlertView * alertView  =[[UIAlertView alloc]initWithTitle:nil message:@"确定已完成该任务？\n（对方确认并评价后即可完成任务）" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alertView.tag = 33;
    [alertView show];

}

#pragma mark - UIAlertViewDelegate Method
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag ==22 && buttonIndex ==1) {
        
        WeakSelf;
        [[PTCoreManager shareInstance]performPostMissionDeleteWithUUID:self.missionDetailModel.uuid Completion:^(PTMJsonResult * retObject, NSError * _Nullable retError) {
            if (retObject) {
                
                if ([retObject.code integerValue] == kPTCore_Response_Code) {
                    
                    UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"已删除" delegate:weakSelf cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    alertView.tag = 1111;
                    [alertView show];
                }
            }
            else
            {
                UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"提交错误" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alertView show];
            }
            
        }];
    }
    else if (alertView.tag ==33 && buttonIndex ==1)
    {
        SINAUserModel *userModel = [[SINACommonUserHelper share]getUserModel];
        
        WeakSelf;
        [[PTCoreManager shareInstance]performPostMissionFinishWithUserName:userModel.userRealName serviceOwnerUUID:self.missionDetailModel.employer.uuid serviceUUID:self.missionDetailModel.serviceUUID type:[self.missionDetailModel.type integerValue] missionUUID:self.missionDetailModel.uuid Completion:^(PTMJsonResult * retObject, NSError * _Nullable retError) {
            
            if (retObject) {
                
                if ([retObject.code integerValue] == kPTCore_Response_Code) {
                    
                    [weakSelf requestData];
                    
                    UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"已完成" delegate:weakSelf cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    alertView.tag = 2222;
                    [alertView show];
                }
            }
            else
            {
                UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"提交错误" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alertView show];
            }
        }];
    }
    else if (alertView.tag ==1111 && buttonIndex ==0)
    {
        //已删除
        [[NSNotificationCenter defaultCenter]postNotificationName:kPT_Global_Notification_RefreshMyPartJobList object:nil];
        
        [self.navigationController popViewControllerAnimated:YES];
    }
    else if (alertView.tag == 2222 && buttonIndex == 0)
    {
        //已完成
       
        [[NSNotificationCenter defaultCenter]postNotificationName:kPT_Global_Notification_RefreshMyPartJobList object:nil];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
